package com.ruoyi.web.controller.production;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.production.domain.vo.ProductionAndSaleVo;
import com.ruoyi.production.domain.vo.SaleOrderDetailVo;
import org.apache.el.parser.Token;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.production.domain.ProductionPlan;
import com.ruoyi.production.service.IProductionPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生产计划Controller
 *
 * @author jgb
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/production/plan")
public class ProductionPlanController extends BaseController
{
    @Autowired
    private IProductionPlanService productionPlanService;

    /**
     * 计划列表
     */
    @PreAuthorize("@ss.hasPermi('production:plan:list')")
    @GetMapping("/getPlanList")
    public TableDataInfo getPlanList(ProductionPlan productionPlan)
    {
        return getDataTable(productionPlanService.selectProductionPlanList(productionPlan));
    }

    /**
     * 查询销售以及产品列表
     */
    @PreAuthorize("@ss.hasPermi('production:plan:list')")
    @GetMapping("/getPlans")
    public TableDataInfo getPlans(ProductionPlan productionPlan)
    {
        return getDataTable(productionPlanService.getPlans(productionPlan));
    }


    /**
     * 查询销售以及产品列表
     */
    @PreAuthorize("@ss.hasPermi('production:plan:list')")
    @GetMapping("/getSaleOrders")
    public TableDataInfo getSaleOrders()
    {
        List<SaleOrderDetailVo> list = productionPlanService.getSaleOrders();
        return getDataTable(list);
    }


    /**
     * 查询生产计划列表
     */
    @PreAuthorize("@ss.hasPermi('production:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductionPlan productionPlan, String saleOrderNo, String customerName)
    {
        startPage();
        List<ProductionAndSaleVo> list = productionPlanService.getProductionPlanAndSaleList(productionPlan, saleOrderNo, customerName);
        return getDataTable(list);
    }

    /**
     * 导出生产计划列表
     */
    @PreAuthorize("@ss.hasPermi('production:plan:export')")
    @Log(title = "生产计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductionPlan productionPlan)
    {
        List<ProductionPlan> list = productionPlanService.selectProductionPlanList(productionPlan);
        ExcelUtil<ProductionPlan> util = new ExcelUtil<ProductionPlan>(ProductionPlan.class);
        util.exportExcel(response, list, "生产计划数据");
    }

    /**
     * 获取生产计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productionPlanService.selectProductionPlanById(id));
    }

    /**
     * 新增生产计划
     */
    @PreAuthorize("@ss.hasPermi('production:plan:add')")
    @Log(title = "生产计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductionAndSaleVo productionPlan)
    {
        return toAjax(productionPlanService.insertProductionPlan(productionPlan));
    }

    /**
     * 修改生产计划
     */
    @PreAuthorize("@ss.hasPermi('production:plan:edit')")
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductionAndSaleVo productionPlan)
    {
        return toAjax(productionPlanService.updateProductionPlanAndDetail(productionPlan));
    }

    /**
     * 修改生产计划状态
     */
    @PreAuthorize("@ss.hasPermi('production:plan:edit')")
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    @PutMapping("editPublic")
    public AjaxResult editPublic(@RequestBody ProductionPlan productionPlan)
    {
        return toAjax(productionPlanService.updateProductionPlan(productionPlan));
    }

    /**
     * 删除生产计划
     */
    @PreAuthorize("@ss.hasPermi('production:plan:remove')")
    @Log(title = "生产计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productionPlanService.deleteProductionPlanByIds(ids));
    }
}
